package com.gdones.bootserver.bus.param.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.bootserver.bus.param.dto.Param;
import com.gdones.bootserver.bus.param.dto.ParamDataDto;
import com.gdones.bootserver.bus.param.mapper.ParamMapper;
import com.gdones.bootserver.bus.param.service.IParamService;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典主表，信息描述表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-03-27
 */
@Service
public class ParamServiceImpl extends ServiceImpl<ParamMapper, Param> implements IParamService {

    @Override
    public MyResult getParamPage(Param param, MyPageBand page) {
        MyResult result = new MyResult();

        ParamMapper paramMapper = getBaseMapper();

        IPage<Param> paramListPage = paramMapper.getParamListPage(PageDTO.of(page.getPage(), page.getSize()), param);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("total",paramListPage.getTotal());
        resMap.put("pageData",paramListPage.getRecords());
        resMap.put("page",page.getPage());
        resMap.put("limit",page.getSize());

        result.setMsg("查询成功！");
        result.setData(resMap);

        return result;
    }

    @Override
    public MyResult add(Param param) {
        MyResult result = new MyResult();

        ParamMapper paramMapper = getBaseMapper();

        //先查询有没有同编号字典
        QueryWrapper<Param> wrapper = new QueryWrapper<>();
        wrapper.eq("paramCode",param.getParamCode());

        List<Param> params = paramMapper.selectList(wrapper);
        if(params.size()>0){
            result.setMsg("编号["+param.getParamCode()+"]已存在，请换一个编号。");
            result.setCode("9999");
            return result;
        }

        int insert = paramMapper.insert(param);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(Param param) {
        MyResult result = new MyResult();

        ParamMapper paramMapper = getBaseMapper();

        int i = paramMapper.updateById(param);

        if(i>0){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult delete(String paramIDs) {
        MyResult result = new MyResult();

        ParamMapper paramMapper = getBaseMapper();

        String[] split = paramIDs.split(",");

        List<String> strings = Arrays.asList(split);

        QueryWrapper<Param> queryWrap = new QueryWrapper<Param>();

        queryWrap.in("paramID",strings);

        int delete = paramMapper.delete(queryWrap);

        //删除关联属性表数据
        strings.forEach(id->{
            paramMapper.deleteParamInfosByParamId(id);
        });

        //int update = paramMapper.update(updateParam, queryWrap);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult getParamByCode(String paramCode) {
        MyResult result = new MyResult();

        ParamMapper paramMapper = getBaseMapper();

        List<ParamDataDto> paramDataByCode = paramMapper.getParamDataByCode(paramCode);

        if(paramDataByCode.size()>0){
            result.setMsg("字典查询成功！");
            result.setCode("0000");
            result.setData(paramDataByCode);
        }else{
            result.setMsg("字典查询异常！");
            result.setCode("9999");
            result.setData(paramDataByCode);
        }

        return result;
    }
}
